import { userStore } from '@/stores'
/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
	uni.reLaunch({
		url:'/pages/login/index',
		success: function() {
			uni.removeStorageSync('token');
			const user = userStore()
			user.$reset()
			// #ifdef APP-PLUS
			plus.navigator.closeSplashscreen()
			// #endif
		}
	})
}

/**
 * 验证手机号
 */

export function validatePhoneNumber(str) {
 const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
 return reg.test(str)
}

export function getCharAll() {
	const charAll = []
	/**
	 * 键盘字符表(小写)
	 * 非shift键盘字符表
	 */
	const charTable1 = [
		["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", ""],
		["", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"],
		["", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "", ""],
		["", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "", "", ""]
	]
	/**
	 * shift键盘的字符表
	 */
	const charTable2 = [
		["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", ""],
		["", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "{", "}", "|"],
		["", "a", "s", "d", "f", "g", "h", "j", "k", "l", ":", '"', "", ""],
		["", "z", "x", "c", "v", "b", "n", "m", "<", ">", "?", "", "", ""]
	]
	// 横向所有的值(正序和反序)-
	charTable1.concat(charTable2).forEach((item, index) => {
		charAll.push(item.join(''), Object.assign([], item).reverse().join(''))
	})

	// 反斜杠纵向所有的值(正序和反序)\
	for (let i = 0; i < charTable1[0].length; i++) {
		let sb1 = []
		let sb2 = []
		charTable1.forEach(item => {
			sb1.push(item[i])
		})
		charTable2.forEach(item => {
			sb2.push(item[i])
		})
		// 去除长度小于3的值
		sb1.join('').length < 3 || charAll.push(sb1.join(''), Object.assign([], sb1).reverse().join(''))
		sb1.join('').length < 3 || charAll.push(sb2.join(''), Object.assign([], sb2).reverse().join(''))
	}

	// 正斜杠纵向所有的值(正序和反序)/
	for (let i = 0; i < charTable1[0].length; i++) {
		let sb1 = []
		let sb2 = []
		charTable1.forEach((item, index) => {
			i - index < 0 || sb1.push(item[i - index])
		})
		charTable2.forEach((item, index) => {
			i - index < 0 || sb2.push(item[i - index])
		})
		// 去除长度小于3的值
		sb1.join('').length < 3 || charAll.push(sb1.join(''), Object.assign([], sb1).reverse().join(''))
		sb1.join('').length < 3 || charAll.push(sb2.join(''), Object.assign([], sb2).reverse().join(''))
	}
	charAll.push('abcdefghijklmnopqrstuvwxyz')
	return charAll
}

export function validateChar(val) {
	const charAll = getCharAll();
	let password = val.toLowerCase();
	// 密码3位比对
	for (let i = 0; i < password.length - 2; i++) {
		let n1 = password[i];
		let n2 = password[i + 1];
		let n3 = password[i + 2];
		// 判断重复字符
		if (n1 === n2 && n1 === n3) {
			return false;
			// 判断键盘连续字符
		} else if (charAll.some(item => {
				return item.includes(n1 + n2 + n3);
			})) {
			return false
		}
	}
	return true
}